package
{
	import Classes.ActionManager;
	import Classes.ComponentManager;
	import Classes.Components.RigidBodyComponent;
	import Classes.ConfigManager;
	import Classes.Entity;
	import Classes.PerfTracker;
	import Classes.Prototypes;
	import Classes.UserInterface;
	import Classes.World;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.getTimer;

	public class Main extends Sprite
	{
		public static var P2WSCALE:int = 100;

		public static var configManager:ConfigManager = new ConfigManager;
		public static var actionManager:ActionManager;
		public static var componentManager:ComponentManager;
		public static var prototypes:Prototypes;
		public static var userInterface:UserInterface;
		public static var perfTracker:PerfTracker;
		
		
		private var m_world:World;

		private var m_lastTick:int;
		public function Main(){
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.frameRate = 60;
			
			configManager.load('gameConfig.xml');
			configManager.addEventListener(Event.COMPLETE, onConfigLoadComplete);
		}
		
		private function onConfigLoadComplete(e:Event):void{
			m_world = new World;
			componentManager = new ComponentManager;
			actionManager = new ActionManager;
			perfTracker = new PerfTracker;
			prototypes = new Prototypes;
			userInterface = new UserInterface;
			
			actionManager.setWorld(m_world);
			userInterface.setWorld(m_world);
			userInterface.setStage(stage);
			
			addChild(userInterface.getDisplay());
			
			m_world.setPlayer(m_world.entityManager.createPlayer());
			
			addChild(perfTracker.getDisplay());
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public static function stage2world(point:Point):Point{
			return new Point(point.x/P2WSCALE,point.y/P2WSCALE);
		}
		
		
		private function onEnterFrame(e:Event):void{
			var delta:int = getTimer() - m_lastTick;
			m_lastTick = getTimer();
			perfTracker.update(delta);
		}		
		
	}
	
}
